package com.work.game.ad;

import android.widget.FrameLayout;

import com.bytedance.sdk.openadsdk.AdSlot;
import com.bytedance.sdk.openadsdk.CSJAdError;
import com.bytedance.sdk.openadsdk.CSJSplashAd;
import com.bytedance.sdk.openadsdk.TTAdNative;
import com.bytedance.sdk.openadsdk.TTAdSdk;
import com.bytedance.sdk.openadsdk.mediation.manager.MediationAdEcpmInfo;
import com.bytedance.sdk.openadsdk.mediation.manager.MediationBaseManager;
import com.work.game.ad.inter.RewardAdSplasInter;
import com.work.game.utils.UIUtils;
import com.work.game.volley.BaseActivity;

import cn.hutool.core.util.ObjectUtil;

/**
 * 开屏广告
 */
public class MediationSplashAd {
    private BaseActivity activity;
    private String codeId;

    private RewardAdSplasInter rewardAdSplasInter;
    private CSJSplashAd csjSplashAd;

    private MediationSplashAd() {
    }

    private static volatile MediationSplashAd instance;

    public static synchronized MediationSplashAd getInstance() {
        if (ObjectUtil.isNull(instance)) {
            synchronized (MediationSplashAd.class) {
                if (ObjectUtil.isNull(instance)) {
                    instance = new MediationSplashAd();
                }
            }
        }
        return instance;
    }

    public void init(BaseActivity activity, String codeId) {
        this.activity = activity;
        this.codeId = codeId;
    }

    public void setActivity(BaseActivity activity) {
        this.activity = activity;
    }

    public void setRewardAdSplasInter(RewardAdSplasInter rewardAdSplasInter) {
        this.rewardAdSplasInter = rewardAdSplasInter;
    }

    //构造开屏广告的Adslot
    private AdSlot buildSplashAdslot() {
        return new AdSlot.Builder().setCodeId(codeId) //广告位ID
                .setAdCount(3).setImageAcceptedSize(UIUtils.getScreenWidthInPx(activity), UIUtils.getScreenHeightInPx(activity))  //设置广告宽高 单位px
                .build();
    }


    // 加载开屏广告
    public void loadCacheSplashAd() {
        TTAdNative adNativeLoader = TTAdSdk.getAdManager().createAdNative(activity);
        adNativeLoader.loadSplashAd(buildSplashAdslot(), new TTAdNative.CSJSplashAdListener() {

            @Override
            public void onSplashLoadSuccess(CSJSplashAd csjSplashAd) {
                //广告加载成功
            }

            @Override
            public void onSplashLoadFail(CSJAdError csjAdError) {
                //广告加载失败
                if (ObjectUtil.isNotNull(rewardAdSplasInter)) {
                    rewardAdSplasInter.onError();
                }
            }

            @Override
            public void onSplashRenderSuccess(CSJSplashAd csjSplashAd) {
                //广告渲染成功，在此展示广告
                getInstance().csjSplashAd = csjSplashAd;
            }

            @Override
            public void onSplashRenderFail(CSJSplashAd csjSplashAd, CSJAdError csjAdError) {
                //广告渲染失败
                if (ObjectUtil.isNotNull(rewardAdSplasInter)) {
                    rewardAdSplasInter.onError();
                }
            }
        }, 3500);
    }


    //展示开屏广告
    public void showCacheSplashAd(FrameLayout container) {
        if (csjSplashAd == null || container == null) {
            loadSplashAd(container);
            return;
        }
        activity.loadDismiss();
        csjSplashAd.setSplashAdListener(new CSJSplashAd.SplashAdListener() {
            @Override
            public void onSplashAdShow(CSJSplashAd csjSplashAd) {
                //广告展示
                //获取展示广告相关信息，需要再show回调之后进行获取
                MediationBaseManager manager = csjSplashAd.getMediationManager();
                if (manager != null && manager.getShowEcpm() != null) {
                    MediationAdEcpmInfo showEcpm = manager.getShowEcpm();
                    String ecpm = showEcpm.getEcpm(); //展示广告的价格
                    String sdkName = showEcpm.getSdkName();  //展示广告的adn名称
                    String slotId = showEcpm.getSlotId(); //展示广告的代码位ID
                }
            }

            @Override
            public void onSplashAdClick(CSJSplashAd csjSplashAd) {
                //广告点击
            }

            @Override
            public void onSplashAdClose(CSJSplashAd csjSplashAd, int i) {
                //广告关闭
                rewardAdSplasInter.onAdClose();
                onDestroy();
                loadCacheSplashAd();
            }
        });

        csjSplashAd.showSplashView(container);//展示开屏广告
    }

    // 加载开屏广告
    private void loadSplashAd(FrameLayout container) {
        TTAdNative adNativeLoader = TTAdSdk.getAdManager().createAdNative(activity);

        adNativeLoader.loadSplashAd(buildSplashAdslot(), new TTAdNative.CSJSplashAdListener() {

            @Override
            public void onSplashLoadSuccess(CSJSplashAd csjSplashAd) {
                //广告加载成功
            }

            @Override
            public void onSplashLoadFail(CSJAdError csjAdError) {
                //广告加载失败
                rewardAdSplasInter.onError();
            }

            @Override
            public void onSplashRenderSuccess(CSJSplashAd csjSplashAd) {
                //广告渲染成功，在此展示广告
                getInstance().csjSplashAd = csjSplashAd;
                showSplashAd(container); //注 ：splashContainer为展示Banner广告的容器
            }

            @Override
            public void onSplashRenderFail(CSJSplashAd csjSplashAd, CSJAdError csjAdError) {
                //广告渲染失败
                rewardAdSplasInter.onError();
            }
        }, 3500);
    }


    //展示开屏广告
    private void showSplashAd(FrameLayout container) {
        if (csjSplashAd == null || container == null) {
            return;
        }
        activity.loadDismiss();
        csjSplashAd.setSplashAdListener(new CSJSplashAd.SplashAdListener() {
            @Override
            public void onSplashAdShow(CSJSplashAd csjSplashAd) {
                //广告展示
                //获取展示广告相关信息，需要再show回调之后进行获取
                MediationBaseManager manager = csjSplashAd.getMediationManager();
                if (manager != null && manager.getShowEcpm() != null) {
                    MediationAdEcpmInfo showEcpm = manager.getShowEcpm();
                    String ecpm = showEcpm.getEcpm(); //展示广告的价格
                    String sdkName = showEcpm.getSdkName();  //展示广告的adn名称
                    String slotId = showEcpm.getSlotId(); //展示广告的代码位ID
                }
            }

            @Override
            public void onSplashAdClick(CSJSplashAd csjSplashAd) {
                //广告点击
            }

            @Override
            public void onSplashAdClose(CSJSplashAd csjSplashAd, int i) {
                rewardAdSplasInter.onAdClose();
                onDestroy();
                loadCacheSplashAd();
            }
        });
        csjSplashAd.showSplashView(container);//展示开屏广告
    }

    //激励视频 end
    private void onDestroy() {
        if (csjSplashAd != null && csjSplashAd.getMediationManager() != null) {
            csjSplashAd.getMediationManager().destroy();
        }
        csjSplashAd = null;
    }

}
